package org.example.rubbish.controller;

import org.example.rubbish.entity.Site;
import org.example.rubbish.service.SiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/sites")
@CrossOrigin(origins = "*")
public class SiteController {

    @Autowired
    private SiteService siteService;

    /**
     * 获取所有有效的站点列表
     */
    @GetMapping("/active")
    public AlarmController.ApiResponse<List<Site>> getActiveSites() {
        try {
            List<Site> sites = siteService.getAllActiveSites();
            return AlarmController.ApiResponse.success(sites);
        } catch (Exception e) {
            return AlarmController.ApiResponse.error("获取站点列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取站点信息
     */
    @GetMapping("/{id}")
    public AlarmController.ApiResponse<Site> getSiteById(@PathVariable Long id) {
        try {
            Site site = siteService.getSiteById(id);
            if (site != null) {
                return AlarmController.ApiResponse.success(site);
            } else {
                return AlarmController.ApiResponse.error("站点不存在");
            }
        } catch (Exception e) {
            return AlarmController.ApiResponse.error("获取站点信息失败: " + e.getMessage());
        }
    }
}
